import requests
import json
import os

# 接口配置
url = "https://xhx.kaike.la/member/index/cancelaccount"
headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "xhxtoken=00002771_13cd36f38c065dec73dfde2123444bbc229"
}


def cancel_account(member_id_list):
    """批量移除成员"""
    results = []

    for member_id in member_id_list:
        payload = {
            "member_id": member_id
        }

        try:
            response = requests.post(url, data=payload, headers=headers)
            response.raise_for_status()  # 检查请求是否成功

            result = response.json()
            results.append(result)

            # 打印请求信息（调试用）
            print(f"处理成员ID: {member_id}")
            print(f"Request URL: {response.request.url}")
            print(f"Status Code: {response.status_code}")
            print(f"Response: {result}")
            print("-" * 50)

        except requests.exceptions.RequestException as e:
            print(f"处理成员ID {member_id} 时出错: {e}")
            results.append({"error": str(e), "member_id": member_id})

    return results


if __name__ == "__main__":
    # 读取统计目录下的output.json文件
    file_path = os.path.join("output.json")

    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            member_ids = json.load(f)
        print(f"成功从 {file_path} 读取 {len(member_ids)} 个成员ID")
    except FileNotFoundError:
        print(f"错误：找不到文件 {file_path}")
        exit(1)
    except json.JSONDecodeError:
        print(f"错误：文件 {file_path} 不是有效的JSON格式")
        exit(1)

    # 执行批量移除
    final_results = cancel_account(member_ids)

    # 打印最终结果
    print("\n处理完成！最终结果:")
    print(final_results)